package com.zy.edu.config.ali;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;

/**
 * @author: HuangHaiShui
 * @description:
 * @date: 2021/7/27 11:12
 * @version: 1.0
 */
public class AliVideo {
    public  static String KeyId="LTAI5t8NvgBCgDV5Thwgqycw";
    public  static String KeySecret="4OJ7mXYrQ1cMjsuFFTN5467uKpzvjZ";
    public  static String RegionId = "cn-shanghai";
    public  static String SecurityToken = "";

    public static DefaultAcsClient initVodClient() throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile(RegionId, KeyId, KeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    public static DefaultAcsClient initVodClient( String securityToken) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile(RegionId, KeyId, KeySecret, securityToken);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /**
     * 获取视频上传地址和凭证
     * @return CreateUploadVideoResponse 获取视频上传地址和凭证响应数据
     * @throws Exception
     */
    public static CreateUploadVideoResponse createUploadVideo(String title,String fileName) throws Exception {
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        request.setTitle(title);
        request.setFileName(fileName);
        DefaultAcsClient client = initVodClient();
        //UserData，用户自定义设置参数，用户需要单独回调URL及数据透传时设置(非必须)
        //JSONObject userData = new JSONObject();

        //UserData回调部分设置
        //JSONObject messageCallback = new JSONObject();
        //messageCallback.put("CallbackURL", "http://192.168.0.0/16");
        //messageCallback.put("CallbackType", "http");
        //userData.put("MessageCallback", messageCallback.toJSONString());

        //UserData透传数据部分设置
        //JSONObject extend = new JSONObject();
        //extend.put("MyId", "user-defined-id");
        //userData.put("Extend", extend.toJSONString());

        //request.setUserData(userData.toJSONString());

        return client.getAcsResponse(request);
    }

    /**
     * 刷新视频上传凭证
     * @return RefreshUploadVideoResponse 刷新视频上传凭证响应数据
     * @throws Exception
     */
    public static RefreshUploadVideoResponse refreshUploadVideo(String videoId) throws Exception {
        RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
        request.setVideoId(videoId);
        DefaultAcsClient client = initVodClient();
        return client.getAcsResponse(request);
    }

    //删除视频
    public static DeleteVideoResponse deleteVideo(String videoId) throws Exception {
        DeleteVideoRequest request = new DeleteVideoRequest();
        //支持传入多个视频ID，多个用逗号分隔
//        request.setVideoIds("VideoId1,VideoId2");
        request.setVideoIds(videoId);
        DefaultAcsClient client = initVodClient();
        return client.getAcsResponse(request);
    }

    /*获取播放地址函数*/
    public static GetPlayInfoResponse getPlayInfo(String videoId) throws Exception {
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(videoId);
        DefaultAcsClient client = initVodClient();
        return client.getAcsResponse(request);
    }
    /*获取播放地址函数*/
    public static GetVideoPlayAuthResponse getPlayAuthInfo(String videoId) throws Exception {
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);
        DefaultAcsClient client = initVodClient();
        return client.getAcsResponse(request);
    }



    // 请求示例
    public static void main(String[] argv) {
//        DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
//        CreateUploadVideoResponse response = new CreateUploadVideoResponse();
//        try {
//            response = createUploadVideo(client);
//            System.out.print("VideoId = " + response.getVideoId() + "\n");
//            System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
//            System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
//        } catch (Exception e) {
//            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
//        }
//        System.out.print("RequestId = " + response.getRequestId() + "\n");
    }
}
